package com.hbu.product.hbumallproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbu.common.utils.PageUtils;
import com.hbu.common.utils.Query;
import com.hbu.product.hbumallproduct.dao.BrandDao;
import com.hbu.product.hbumallproduct.dao.CategoryBrandRelationDao;
import com.hbu.product.hbumallproduct.dao.CategoryDao;
import com.hbu.product.hbumallproduct.entity.BrandEntity;
import com.hbu.product.hbumallproduct.entity.CategoryBrandRelationEntity;
import com.hbu.product.hbumallproduct.entity.CategoryEntity;
import com.hbu.product.hbumallproduct.event.BrandDataSynchronizer;
import com.hbu.product.hbumallproduct.event.BrandEvent;
import com.hbu.product.hbumallproduct.event.CategoryDataSynchronizer;
import com.hbu.product.hbumallproduct.event.CategoryEvent;
import com.hbu.product.hbumallproduct.service.CategoryBrandRelationService;
import com.hbu.product.hbumallproduct.utils.ConnectionContext;
import com.hbu.product.hbumallproduct.utils.SqlUtil;
import com.hbu.product.hbumallproduct.vo.BrandListVO;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author 90458
 */
@Service("categoryBrandRelationService")
@AllArgsConstructor
@Slf4j
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService, BrandDataSynchronizer, CategoryDataSynchronizer {

    private static final String TABLE_NAME = "pms_category_brand_relation";

    private CategoryDao categoryDao;

    private BrandDao brandDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public int saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        CategoryEntity category = categoryDao.selectById(categoryBrandRelation.getCatelogId());
        BrandEntity brand = brandDao.selectById(categoryBrandRelation.getBrandId());
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());
        return this.baseMapper.insert(categoryBrandRelation);
    }

    @Override
    public List<BrandListVO> getBrandList(Long catId) {
        List<Long> brandIdsByCatId = baseMapper.getBrandIdsByCatId(catId);
        return brandDao.selectBatchIds(brandIdsByCatId).stream().map(BrandListVO::new).collect(Collectors.toList());
    }

    @SneakyThrows
    @Override
    public void syncBrandData(BrandEvent brandEvent) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandEvent.getBrand().getBrandId());
        categoryBrandRelationEntity.setBrandName(brandEvent.getBrand().getName());
        Connection connection = ConnectionContext.getConnection();
        SQL updateSql = new SQL().UPDATE(TABLE_NAME);
        SqlUtil.updateUtil(categoryBrandRelationEntity, updateSql);
        updateSql.WHERE("brand_id = " + brandEvent.getBrand().getBrandId());
        log.debug("执行的sql语句为 " + updateSql);
        PreparedStatement preparedStatement = connection.prepareStatement(updateSql.toString());
        preparedStatement.executeUpdate();
        preparedStatement.close();
        brandEvent.getCountDownLatch().countDown();
    }

    @SneakyThrows
    @Override
    public void syncCategoryData(CategoryEvent categoryEvent) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogId(categoryEvent.getCategory().getCatId());
        categoryBrandRelationEntity.setCatelogName(categoryEvent.getCategory().getName());
        Connection connection = ConnectionContext.getConnection();
        SQL updateSql = new SQL().UPDATE(TABLE_NAME);
        SqlUtil.updateUtil(categoryBrandRelationEntity, updateSql);
        updateSql.WHERE("catelog_id = " + categoryEvent.getCategory().getCatId());
        log.debug("执行的sql语句为 " + updateSql);
        PreparedStatement preparedStatement = connection.prepareStatement(updateSql.toString());
        preparedStatement.executeUpdate();
        preparedStatement.close();
        categoryEvent.getCountDownLatch().countDown();
    }

}